package com.bos.report.factory;

import com.bos.report.model.ReportModel;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 5/22/11
 * Time: 8:31 PM
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ReportFactory<T extends ReportModel> {

    public final static String KEY_DATASOURCE = "datasource";
    public final static String KEY_DATASOURCE_SIZE = "datasourceSize";

    /**
     * Data Source untuk report
     */
    protected List<T> dataSource;

    /**
     * parameter untuk dikembalikan sebagai response gar bisa digunakan oleh JasperReport/chart
     */
    protected Map responseParameter;

    public ReportFactory() {
        responseParameter = new HashMap();
    }

    /**
     * Dapatkan data source.
     * @return
     */
    public List<T> getDataSource() {
        return dataSource;
    }

    /**
     * Set datasource yang baru.
     * @param dataSource
     */
    public void setDataSource(List<T> dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * Dapatkan Response parameter.
     * @return
     */
    public Map getResponseParameter() {
        responseParameter.put(KEY_DATASOURCE, dataSource);

        return responseParameter;
    }

    /**
     * Tambah response parameter.
     * @param key
     * @param value
     */
    public void putResponseParameter(String key, Object value){
        responseParameter.put(key, value);

        if(key.equals(KEY_DATASOURCE))
            setDataSource((List<T>) value);
    }

    /**
     * Tambah banyak response parameter sekaligus.
     * @param
     */
    public void putAllResponseParameter(Map responseParameter) {
        if(responseParameter == null || responseParameter.isEmpty())
            return;

        if(responseParameter.containsKey(KEY_DATASOURCE))
            setDataSource((List<T>) responseParameter.get(KEY_DATASOURCE));

        this.responseParameter.putAll(responseParameter);
    }
}
